﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using AsyncDBClient.Models;

namespace AsyncDBClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //Print();

            //using (var context = new SydEnergiEntities())
            //{

            //    var cabinet1 = new Cabinet()
            //                      {
            //                          Address =
            //                              new Address()
            //                                  {
            //                                      StreetName = "Bygade",
            //                                      StreetNumber = 40,
            //                                      ZipCode = 2400,
            //                                      City = "København NV"
            //                                  },
            //                          CabinetId = "SK123 FRE",
            //                          Latitude = 55.5555,
            //                          Longitude = 12.5555
            //                      };
            //    var cabinet2 = new Cabinet()
            //    {
            //        Address =
            //            new Address()
            //            {
            //                StreetName = "Bygmestervej",
            //                StreetNumber = 55,
            //                ZipCode = 2400,
            //                City = "København NV"
            //            },
            //        CabinetId = "SK107 HAC",
            //        Latitude = 55.5555,
            //        Longitude = 12.5555
            //    };

            //    var cabinets = new List<Cabinet> { cabinet1, cabinet2 };

            //    foreach (var cabinet in cabinets)
            //    {
            //        context.Cabinets.AddObject(cabinet);
            //    }
            //    context.SaveChanges();
            //}

            Print();

            Console.ReadLine();
        }

        private static void Print()
        {
            using (var context = new SydEnergiEntities())
            {
                var cabinets = context.Cabinets.Include("Address").Include("Entry");
                foreach (var cabinet in cabinets)
                {
                    Console.WriteLine(cabinet.Id + " {0} {1} {2} {3} {4} {5} \n\n", cabinet.Address.StreetName,
                                      cabinet.Address.StreetNumber, cabinet.Address.ZipCode,
                                      cabinet.CabinetId, cabinet.Latitude, cabinet.Longitude);
                }
            }
        }
    }
}




/*


                //context.AddToCabinets(cabinet);
                //context.SaveChanges();

                //var cabinets = context.Cabinets;
                //if (cabinets.Any(o => o.Id == 2))
                //{
                //    var objectToDelete = cabinets.Single(o => o.Id == 2);
                //    var otherObjectToDelete = objectToDelete.Address;
                //    context.DeleteObject(objectToDelete);
                //    context.DeleteObject(otherObjectToDelete);
                //}
                foreach (var cabinet in cabinets)
                {
                    var cabinetsDb = context.Cabinets;
                    if (cabinetsDb.Any(c => c.CabinetId == cabinet.CabinetId))
                    {
                        var key = new EntityKey("SydEnergiDbContainer.Cabinets", "CabinetId", cabinet.CabinetId);
                        var objectToUpdate = cabinets.Single(o => o.CabinetId == cabinet.CabinetId);
                        if (objectToUpdate != null)
                        {
                            objectToUpdate = cabinet;
                            context.SaveChanges();
                        }
                    }
                    else
                    {
                        context.Cabinets.AddObject(cabinet);
                    }
                }


*/